//
//  MqForumModuleDetailViewController.m
//  Meiqi
//
//  Created by lanou on 16/1/17.
//  Copyright © 2016年 KFC. All rights reserved.
//

#import "MqForumModuleDetailViewController.h"
#import "MqForumMainModuleCell.h"
#import "MqForumModuleCommentViewController.h"
@interface MqForumModuleDetailViewController ()
<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) NSString *netAPI;

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, assign) NSInteger startPage;
@end

@implementation MqForumModuleDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    //  初始化
    _netAPI = [NSString stringWithFormat:@"http://bbs.biketo.com/api/mobile/index.php?fid=%@&module=forumdisplay&page=1&version=4", _fid];
    _startPage = 1;
    _dataArray = [NSMutableArray array];

    //  取消scrollView的自动布局
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self addSubViews];
    
    [self dataProcess];
    
}

- (void)addSubViews{
    
    //  最新
    UIButton *newButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    newButton.frame = CGRectMake(0, 64, kScreenWidth / 2, 40);
    [newButton setTitle:@"最新" forState:(UIControlStateNormal)];
    [newButton addTarget:self action:@selector(newButton:) forControlEvents:(UIControlEventTouchUpInside)];
//    newButton.backgroundColor = [UIColor redColor];
    [self.view addSubview:newButton];
    
    //  精华
    UIButton *creamButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    creamButton.frame = CGRectMake(newButton.right, newButton.top, newButton.width, 40);
//    creamButton.backgroundColor = [UIColor yellowColor];
    [creamButton setTitle:@"最新" forState:(UIControlStateNormal)];
    [creamButton addTarget:self action:@selector(creamButton:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:creamButton];
    
    //  tableView
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, newButton.bottom, kScreenWidth, kScreenHeight - newButton.height - 64) style:(UITableViewStylePlain)];
    
    //  设置代理
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.rowHeight = 150;
    [self.view addSubview:_tableView];
    
    //  注册cell
    [_tableView registerClass:[MqForumMainModuleCell class] forCellReuseIdentifier:@"MqForumMainModuleCell"];
    
}


//  button点击方法
- (void)newButton:(UIButton *)button{
    
    _netAPI = [NSString stringWithFormat:@"http://bbs.biketo.com/api/mobile/index.php?fid=%@&module=forumdisplay&page=%ld&version=4", _fid, (long)_startPage];
    [self dataProcess];
    
}

- (void)creamButton:(UIButton *)button{
    
    _netAPI = [NSString stringWithFormat:@"http://bbs.biketo.com/api/mobile/index.php?version=4&page=%ld&module=forumdisplay&filter=digest&fid=%@&digest=1",(long)_startPage, _fid];
    
    [self dataProcess];
}

//  数据处理
- (void)dataProcess{
    
    [PKRequestManager requestWithUrlString:_netAPI parDic:nil Method:GET finish:^(NSData *data) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        
        NSArray *listArray = dic[@"Variables"][@"forum_threadlist"];
        
        if (!listArray || [listArray isKindOfClass:[NSNull class]]) {
            
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"暂无数据" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
            [alert show];
            
            
            [self performSelector:@selector(gotoBack:) withObject:alert afterDelay:1];
            
            return ;
            
        }
        if (dic != nil) {
            
            if (_startPage == 1) {
                
                [_dataArray removeAllObjects];
                
            }
     
            
        }
        
        for (NSDictionary *oneDic in listArray) {
            
            MqForumMainModule *mainModule = [[MqForumMainModule alloc] init];
            [mainModule setValuesForKeysWithDictionary:oneDic];
            
            [_dataArray addObject:mainModule];
        }
        
        //  刷新tableView
        [_tableView reloadData];
        
    } error:^(NSError *error) {
        
    } path:nil];
    
}

- (void)gotoBack:(UIAlertView *)alert{
    
    [alert dismissWithClickedButtonIndex:0 animated:YES];
    [self.navigationController popViewControllerAnimated:YES];
    
}

#pragma mark --- tableView代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _dataArray.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MqForumMainModuleCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MqForumMainModuleCell" forIndexPath:indexPath];
    
    MqForumMainModule *model = _dataArray[indexPath.row];
    
    cell.mainModule = model;
    
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MqForumModuleCommentViewController *commentVC = [[MqForumModuleCommentViewController alloc] init];
    
    MqForumMainModule *mainModule = _dataArray[indexPath.row];
    
    commentVC.mainModel = mainModule;
    
    [self.navigationController pushViewController:commentVC animated:YES];
    
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
